<template>
	<el-dialog v-model="visible" :title="!dataForm.id ? '新增' : '修改' " :close-on-click-modal='false'
		:close-on-press-escape='false' :fullscreen='true'>
		<el-form :model="dataForm" :rules="dataRule" ref="dataFormRef" @keyup.enter.native="dataFormSubmitHandle()"
			label-width="120px">

			<el-form-item label="序号名称" prop="name">
				<el-input size="small" v-model="dataForm.name" :disabled="!!dataForm.id"   placeholder="序号名称"></el-input>
			</el-form-item>
			<el-form-item label="序号编码" prop="type">
				<el-input size="small" v-model="dataForm.type" :disabled="!!dataForm.id"  placeholder="序号编码"></el-input>
			</el-form-item>
			<el-form-item label="总长度" prop="size">
				<el-input size="small" v-model="dataForm.size" placeholder="总长度"></el-input>
			</el-form-item>
			<el-form-item label="前缀" prop="prefix">
				<el-input size="small" v-model="dataForm.prefix" placeholder="前缀"></el-input>
			</el-form-item>
			<el-form-item label="后缀" prop="suffix">
				<el-input size="small" v-model="dataForm.suffix" placeholder="后缀"></el-input>
			</el-form-item>
			<el-form-item label="初始值" prop="seed">
				<el-input size="small" v-model="dataForm.seed" placeholder="初始值"></el-input>
			</el-form-item>
			<el-form-item label="间隔值" prop="interval1">
				<el-input size="small" v-model="dataForm.interval1" placeholder="间隔值"></el-input>
			</el-form-item>
			<el-form-item label="当前值" prop="present">
				<el-input size="small" v-model="dataForm.present" placeholder="当前值"></el-input>
			</el-form-item>
			<el-form-item label="最后更新时间" prop="lasttime">
				<el-input size="small" v-model="dataForm.lasttime" placeholder="最后更新时间"></el-input>
			</el-form-item>
			<el-form-item label="更新人" prop="updater">
				<el-input size="small" v-model="dataForm.updater" placeholder="更新人"></el-input>
			</el-form-item>
			<el-form-item label="更新时间" prop="updatedate">
				<el-input size="small" v-model="dataForm.updatedate" placeholder="更新时间"></el-input>
			</el-form-item>
		</el-form>
		<template #footer>
			<el-button size="small" @click="visible = false">取消</el-button>
			<el-button size="small" type="primary" @click="dataFormSubmitHandle()">确定</el-button>
		</template>
	</el-dialog>
</template>

<script>
	import {
		ref,
		reactive,
		onMounted,
		nextTick,
		computed
	} from 'vue';
	import {
		getSequenceById,
		putSequence,
		postSequence
	} from "@/network/lswms/base/Sequence"
	import {
		ElMessage
	} from 'element-plus'
	import _ from 'lodash'


	export default {
		name: "sequenceAddOrUpdate",
		emits: ["refreshDataList"],
		setup(props, {
			emit
		}) {
			const visible = ref(false);
			const dataForm = ref({
				id: '',
				name: '',
				type: '',
				size: '',
				prefix: '',
				suffix: '',
				seed: '',
				interval1: '',
				present: '',
				lasttime: '',
				creator: '',
				createDate: '',
				updater: '',
				updatedate: ''
			});


			const dataRule = reactive({
				name: [{
					required: true,
					message: '必填项不能为空',
					trigger: 'blur'
				}],
				type: [{
					required: true,
					message: '必填项不能为空',
					trigger: 'blur'
				}],
				size: [{
					required: true,
					message: '必填项不能为空',
					trigger: 'blur'
				}],
				// prefix: [{
				// 	required: true,
				// 	message: '必填项不能为空',
				// 	trigger: 'blur'
				// }],
				// suffix: [{
				// 	required: true,
				// 	message: '必填项不能为空',
				// 	trigger: 'blur'
				// }],
				seed: [{
					required: true,
					message: '必填项不能为空',
					trigger: 'blur'
				}],
				interval1: [{
					required: true,
					message: '必填项不能为空',
					trigger: 'blur'
				}],
				present: [{
					required: true,
					message: '必填项不能为空',
					trigger: 'blur'
				}],
				// lasttime: [{
				// 	required: true,
				// 	message: '必填项不能为空',
				// 	trigger: 'blur'
				// }],
				// updater: [{
				// 	required: true,
				// 	message: '必填项不能为空',
				// 	trigger: 'blur'
				// }],
				// updatedate: [{
				// 	required: true,
				// 	message: '必填项不能为空',
				// 	trigger: 'blur'
				// }]
			})

			const dataFormRef = ref(null)

			const init = () => {
				visible.value = true;
				nextTick(function() {
					dataFormRef.value.resetFields();
					if (dataForm.value.id) {
						getsequenceInfo();
					}

				})
			}

			const getsequenceInfo = () => {
				getSequenceById(dataForm.value.id).then(function(res) {
					if (res.code !== 0) {
						return ElMessage.error(res.msg);
					}
					dataForm.value = _.assignIn({}, dataForm.value, res.data);
				}).catch((err) => {});
			}

			// 表单提交
			const dataFormSubmitHandle = _.debounce(() => {
				dataFormRef.value.validate(function(valid) {
					if (!valid) {
						return false;
					}

					let qing = () => {
						return !dataForm.value.id ? postSequence(dataForm.value) : putSequence(dataForm
							.value)
					}
					qing().then(function(res) {
						if (res.code !== 0) {
							return ElMessage.error(res.msg);
						}
						ElMessage({
							message: '操作成功',
							type: 'success',
							duration: 500,
							onClose: function() {
								visible.value = false
								emit('refreshDataList')
							}
						})
					}).catch(function() {});

				})
			}, 1000, {
				'leading': true,
				'trailing': false
			})

			return {
				visible,
				dataForm,
				dataRule,
				init,
				dataFormSubmitHandle,
				dataFormRef

			}
		},


	}
</script>

<style scoped>

</style>
